Evaluates a list of arguments, consisting of an expression followed by a value. The Switch function returns a value that is associated with the expression that is passed by this function.
表式と値のペアを引数として、これら引数のリストを対象とした判定処理を行います。Switch 関数は、与えられた表式がどの引数に該当するかを判定して、対応する値を返します。
Switch (Expression1, Value1[, Expression2, Value2[..., Expression_n, Value_n]])
Switch (Expression1, Value1[, Expression2, Value2[..., Expression_n, Value_n]])
The Switch function evaluates the expressions from left to right, and then returns the value that is assigned to the function expression. If expression and value are not given as a pair, a runtime error occurs.
Switch 関数は、表式を左から右の順番で判定してゆき、関数に与えられた表式に該当する値を返します。表式と値がペアとして指定されていない場合は、実行時エラーが発生します。
Expression: The expression that you want to evaluate.
Expression: 評価の対象となる表式。
Value: The value that you want to return if the expression is True.
Value: 表式が該当した場合に返す値。
In the following example, the Switch function assigns the appropriate gender to the name that is passed to the function:
下記の例では、Switch 関数により、与えられた名前の性別を判定させています。
Sub ExampleSwitch
Sub ExampleSwitch
Dim sGender As String
Dim sGender As String
sGender = GetGenderIndex( "John" )
sGender = GetGenderIndex( "John" )
MsgBox sGender
MsgBox sGender
End Sub
End Sub
Function GetGenderIndex (sName As String) As String
Function GetGenderIndex (sName As String) As String
GetGenderIndex = Switch(sName = "Jane", "female", sName = "John", "male")
GetGenderIndex = Switch(sName = "Jane", "female", sName = "John", "male")
End Function
End Function